package com.tcwlkj.uuService.widget.chatview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.utils.Tools;


/**
 * Created by ${tanbo} on 2017/8/10 0010.
 */

public class ChatBottomView extends LinearLayout implements View.OnClickListener{
    private String TAG = "ChatBottomView";

    public static interface Callback {

        /**
         * 发送消息
         */
        void onSendMessage(CharSequence value, int type);

        /**
         * 点击了显示更多功能的按钮
         */
        void onMoreClick();

        void onLeftButtonClick();

    }
    /**
     * 文本输入容器
     */
    private ChatBottomTextInputView textInputContainer;


    private Callback callback;

    /**
     * 底部透明蒙层
     */
    private LinearLayout chat_layout_penetrate;

    /**
     * 最后一次会话聊天输入方式 0：文本，默认值 1：语音
     */




    /**
     * 文本输入
     */
    private ChatBottomTextInputView.Callback textInputCallback = new ChatBottomTextInputView.Callback() {
        @Override
        public void onTextMoreClick() {
            if (callback != null) {
                callback.onMoreClick();
            }
        }
        @Override
        public void onSendMessage(CharSequence value, int type) {
            if (callback != null) {
                callback.onSendMessage(value, type);
            }

        }

        @Override
        public void onLeftButtonClick() {
            if (callback != null) {
                callback.onLeftButtonClick();
            }
        }

        @Override
        public void onTextInputFocusChange(View v, boolean hasFocus) {

        }
    };

    /**
     * 隐藏公共账号的菜单控件按钮
     */
    private View publicMenuHiddenButton;

    /**
     * 公共账号的菜单控件按钮
     */
    private ViewGroup publicMenuContainer;

    private ViewGroup publicMenuItemsContainer;

    private void initPublicMenu() {

        publicMenuHiddenButton = findViewById(R.id.chat_public_menu_hidden_btn);
        publicMenuHiddenButton.setOnClickListener(this);
        publicMenuContainer = (ViewGroup) findViewById(R.id.chat_public_menu_container);
        publicMenuItemsContainer = (ViewGroup) findViewById(R.id.chat_public_menu_items);
    }

    /**
     * 输入控件容器，包含文本和语音输入
     */
    private ViewGroup chatInputContainer;

    public ChatBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }




    public ChatBottomView(Context context) {
        super(context);
        init();
        initPublicMenu();
    }
    public ChatBottomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
        initPublicMenu();
    }

    private void init() {
        View.inflate(getContext(), R.layout.chat_content_bottom, this);

        chatInputContainer = (ViewGroup) findViewById(R.id.chat_input_container);
        textInputContainer = (ChatBottomTextInputView) findViewById(R.id.character_text_container);

        chat_layout_penetrate = (LinearLayout) findViewById(R.id.chat_layout_penetrate);
        //快捷回复
    }

    public void setCallback(Callback callback) {
        this.callback = callback;

        textInputContainer.setCallback(textInputCallback);

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * 获取输入框控件
     */
    public EditText getInputEditText() {
        return textInputContainer.getInputEditText();
    }

    /**
     * 清除文本输入框的文字
     */
    public void clearSendText() {
        textInputContainer.clearSendText();
    }

    /**
     * 获取输入框正文文字
     */
    public String getInputContentText() {
        return textInputContainer.getInputContentText();
    }



    /**
     * 设置输入框的文字
     */
    public void setInputContentText(String text) {
        textInputContainer.setInputContentText(text);
    }

    /**
     * 强制显示输入键盘,
     *
     * @since 不建议使用
     */
    public void showSoftKeyBoardView() {
        textInputContainer.showSoftKeyBoardView();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.chat_public_menu_hidden_btn:
                setPublicMenuVisible(false);
                break;
            default:
                break;
        }
    }

    /**
     * 控制公众号菜单显示隐藏
     *
     * @param isVisible true:显示公众号菜单,隐藏输入界面; false:显示输入界面,隐藏公众号菜单;
     */
    public void setPublicMenuVisible(boolean isVisible) {
        Tools.setVisibilitySafe(publicMenuContainer, isVisible ? VISIBLE : GONE);
        Tools.setVisibilitySafe(chatInputContainer, isVisible ? GONE : VISIBLE);
    }

}
